import {
  txList,
  getTxCount
} from '../../config/apiServe/new-rpc'
export default {
  props: {
    addr: '',
    token: '',
    pages: {
      type: Object,
      default: () => {
        return {
          currentPage: 1,
          pageSize: 10,
          total: 10
        }
      }
    }
  },
  data() {
    return {
      Trades: [],
      balance: 0,
      txParams: [{
        page: {
          number: 1,
          size: 10
        },
        sort: [{
          "key": "height",
          "ascending": false
        }],
        "match": [{
          "key": "options.symbol",
          "value": this.$route.query.TokenName
        }]
      }],
      txCountParams: [{
        "match": [{
          "key": "options.symbol",
          "value": this.$route.query.TokenName
        }]
      }]
    }
  },
  watch: {
    addr(val) {
      this.$router.push({
        path: '/tokenDetail',
        query: {
          TokenName: this.token,
          adr: val
        }
      })
      if (this.addr !== '') {
        let match_one = [{
            "key": "from",
            "value": val
          },
          {
            "key": "to",
            "value": val
          }
        ]
        this.txCountParams[0].match_one = this.txParams[0].match_one = match_one
      } else {
        this.txCountParams[0].match_one = this.txParams[0].match_one = []
      }
      this.init()
    },
    pages: {
      handler(newPages, oldPages) {
        this.txParams[0].page.number = newPages.currentPage
        this.txParams[0].page.size = newPages.pageSize
        this.getTxList()
      },
      deep: true
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getTxC()
      this.getTxList()
      if (this.addr !== '') this.getTokenBalance()
    },
    // 获取交易总数
    async getTxC() {
      const res = await getTxCount(this.txCountParams)
      if (res.error === null) {
        this.pages.total = res.result
      }
    },
    async getTxList() {
      // this.Trades = []
      const res = await txList(this.txParams)
      if (res.error === null) {
        this.Trades = res.result
      } else {
        this.$notify.error({
          title: this.$t('components.tip'),
          message: this.$t('components.errRequest')
        })
      }
    },
    // 获取地址下token余额
    getTokenBalance() {
      this.Rpc.getAddrTokenAssets(this.addr, 'token').then(res => {
        if (res.error === null) {
          res.result.tokenAssets.forEach(item => {
            if (item.symbol === this.token) this.balance = parseFloat((item.account.balance / 100000000).toFixed(4))
          })
        } else {
          this.balance = 0
        }
      })
    },
    success() {
      this.$message.success(this.$t('components.copySuc'));
    },
    error() {
      this.$message.warning(this.$t('components.copyErr'));
    }
  }
}